// FindAll.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "FindAll.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//
//TODO: If this DLL is dynamically linked against the MFC DLLs,
//		any functions exported from this DLL which call into
//		MFC must have the AFX_MANAGE_STATE macro added at the
//		very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

// CFindAllApp

BEGIN_MESSAGE_MAP(CFindAllApp, CWinApp)
END_MESSAGE_MAP()


// CFindAllApp construction

CFindAllApp::CFindAllApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CFindAllApp object

CFindAllApp theApp;


// CFindAllApp initialization

BOOL CFindAllApp::InitInstance()
{
	CWinApp::InitInstance();

#ifdef _DEBUG
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF );
#endif
	_ASSERTE( _ETLData.m_hInstance == NULL );
	_ETLData.m_hInstance = m_hInstance;

	return TRUE;
}

int CFindAllApp::ExitInstance()
{
	_ASSERTE( _ETLData.m_pETLFrameMap == NULL );

	return CWinApp::ExitInstance();
}


INT_PTR CALLBACK FindDlgProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	BOOL bResult = FALSE;
	switch( msg ){
	case WM_INITDIALOG:
		{
			/*CMyFrame* pFrame = static_cast<CMyFrame*>(GetFrame( hwnd ));
			bResult = pFrame->OnInitDialog( hwnd );*/
		}
		break;
	case WM_COMMAND:
		{
			FindAll* ptr_obj = static_cast<FindAll*>(GetFrame( hwnd ));

			switch( LOWORD( wParam ) )
			{
			case IDC_SEARCH:
				ptr_obj->search( hwnd );
				break;
			}
		}
		break;
	case WM_SIZE:
		{
			RECT rcDlg, rcChild;
			HWND hwndChild;
			if( GetClientRect( hwnd, &rcDlg ) ){
				int cxDlg = rcDlg.right;
				int cyDlg = rcDlg.bottom;
				hwndChild = GetDlgItem( hwnd, IDC_SEARCHTEXT );
				GetWindowRect( hwndChild, &rcChild );
				ScreenToClient( hwnd, (POINT*)&rcChild.left );
				ScreenToClient( hwnd, (POINT*)&rcChild.right );
				int cxEdge = rcChild.left;
				SetWindowPos( hwndChild, NULL, 0, 0, cxDlg - cxEdge * 2, rcChild.bottom - rcChild.top, SWP_NOMOVE | SWP_NOZORDER );

				hwndChild = GetDlgItem( hwnd, IDC_SEARCH );
				GetWindowRect( hwndChild, &rcChild );
				ScreenToClient( hwnd, (POINT*)&rcChild.left );
				ScreenToClient( hwnd, (POINT*)&rcChild.right );
				SetWindowPos( hwndChild, NULL, cxDlg - (rcChild.right - rcChild.left) - cxEdge, rcChild.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER );
			}
		}
		break;
	case WM_NOTIFY:
		{
			NMHDR* pnmh = (NMHDR*)lParam;
			switch( pnmh->code ){
				//			case NM_DBLCLK:
			case NM_CLICK:
			case LVN_ITEMACTIVATE:
				{
					/*NMITEMACTIVATE* pNMItemActivate = (NMITEMACTIVATE*)pnmh;
					if( pNMItemActivate->iItem >= 0 ){
						CMyFrame* pFrame = static_cast<CMyFrame*>(GetFrame( hwnd ));
						pFrame->OnJump( pNMItemActivate->iItem );
					}*/
				}
				break;
			}
		}
		break;
	case WM_CLOSE:
		{
			FindAll* ptr_obj = static_cast<FindAll*>(GetFrame( hwnd ));

			if( ptr_obj )
				ptr_obj->close_searchresultdlg();
		}
		break;
	}
	return bResult;

}

void FindAll::search( HWND parent_hwnd )
{
	open_searchresultdlg( m_hWnd );
}

void FindAll::open_searchresultdlg( HWND parent_hwnd )
{
	close_searchresultdlg();

	m_ptr_dlg = new FindResultDialog;
	m_ptr_dlg->Create( IDD_SEARCHRESULTDLG );
	m_ptr_dlg->set_search_parentwnd( parent_hwnd );

	TCHAR search_str[1024];
	memset( search_str , 0 , 1024 * sizeof( TCHAR ) );
	GetDlgItemText( m_hDlg , IDC_SEARCHTEXT , search_str , sizeof( search_str ) );

	bool is_matchcase = IsDlgButtonChecked( m_hDlg , IDC_CHECK1 ) == BST_CHECKED ? true : false;
	bool is_useregex = IsDlgButtonChecked( m_hDlg , IDC_CHECK2 ) == BST_CHECKED ? true : false;

	m_ptr_dlg->start_search( search_str , is_matchcase , is_useregex );

	m_ptr_dlg->ShowWindow( SW_SHOW );
}

void FindAll::close_searchresultdlg()
{
	if( m_ptr_dlg )
	{
		m_ptr_dlg->stop_search();
		m_ptr_dlg->DestroyWindow();
		delete m_ptr_dlg;
		m_ptr_dlg = 0;
	}
}